<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/12/2
 * Time: 12:16
 */
namespace app\models;

use think\Model;

/**
 * Class GradesModel
 * @package app\models
 */
class GradesDetailsModel extends Model
{
    protected $table = 'edu_class_manage_details';

    /**
     * @param $param
     * @return array
     * @throws \think\exception\DbException
     */
    public function getClassManageLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        $type = -1;
        if (isset($param['graduation_status']) && '' !== $param['graduation_status']) {
            $where['graduation_status'] = $param['graduation_status'];
        }

        if (isset($param['type']) && '' !== $param['type']) {
            $type = $param['type'];
        }

        if (isset($param['select_text_grades']) && '' !== $param['select_text_grades']) {
            if(isset($param['stype_grades']) && '' !== $param['stype_grades']){
                if ($param['stype_grades'] == '1'){
                    $where['grades_name'] = ['like', '%'.$param['select_text_grades'].'%'];
                }elseif ($param['stype_grades'] == '2'){
                    $where['teacher'] = ['like', '%'.$param['select_text_grades'].'%'];
                }elseif ($param['stype_grades'] == '3'){
                    $CourseModel = new CourseModel();
                    $where_course = [];
                    $where_course['course_name'] = ['like', '%'.$param['select_text_grades'].'%'];
                    $arr_id = $CourseModel->where($where_course)->column('id');
                    $where['course_id'] = ['in', implode(',',$arr_id)];
                }
            }
        }

        if (isset($param['course_type']) && '' !== $param['course_type']) {
            if($param['course_type'] == '1'){
                $CourseModel = new CourseModel();
                $where_course = [];
                $where_course['course_type'] = 42;
                $arr_id = $CourseModel->where($where_course)->column('id');
                $where['course_id'] = ['in', implode(',',$arr_id)];
            }else if($param['course_type'] == '2'){
                $CourseModel = new CourseModel();
                $where_course = [];
                $where_course['course_type'] = 43;
                $arr_id = $CourseModel->where($where_course)->column('id');
                $where['course_id'] = ['in', implode(',',$arr_id)];
            }else if($param['course_type'] == '3'){
                $CourseModel = new CourseModel();
                $where_course = [];
                $where_course['course_type'] = 41;
                $arr_id = $CourseModel->where($where_course)->column('id');
                $where['course_id'] = ['in', implode(',',$arr_id)];
            }

        }

        //时间段
        if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
            $where['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
        }




        //课程
        if (isset($param['course_id']) && '' !== $param['course_id']) {
            $where['course_id'] = $param['course_id'];
        }

        //班型
        if (isset($param['grades_type']) && '' !== $param['grades_type']) {
            $CourseModel = new CourseModel();
            $course_id_arr = $CourseModel->where(['course_type' => $param['grades_type']])->column('id');
            $GradesDetailsModel = new GradesDetailsModel();
            $grades_id_arr = $GradesDetailsModel->where(['course_id' => ['in', implode(',',$course_id_arr)]])->column('id');
            $where['id'] = ['in', implode(',',$grades_id_arr)];
        }


        if (isset($param['grades_name']) && '' !== $param['grades_name']) {
            $where['grades_name'] = ['like', '%'.$param['grades_name'].'%'];
        }


        //校区
        if (isset($param['campus_id']) && '' !== $param['campus_id']) {
            $where['campus_id'] = $param['campus_id'];
        }else{
            if(get_current_type() == 4 || get_current_type() == 5){
                $where['campus_id'] = get_current_campus_id();
            }elseif (get_current_type() == 2 || get_current_type() == 3){
                $where['campus_id'] = ['in',get_current_campus_id()];
            }
        }
        //获取班级列表
        if(get_current_type() == 5){
            if(get_current_group_type() == 2){  //老师类
                $where['user_teacher_id'] = get_current_id();
            }else if(get_current_group_type() == 3){  //助教类
                $where['user_assistant_id'] = get_current_id();
            }else if(get_current_group_type() == 4){ //班主任
                $where['user_master_id'] = get_current_id();
            }else if(get_current_group_type() == 5){ //教务类
                $where['user_edu_id'] = get_current_id();
            }
        }



        $where_elimination_gardes = [];
        $where_elimination_gardes['status'] = 1;
        $where_elimination_gardes['give_status'] = 1;


        $where_elimination_gardes_arrears = [];
        $where_elimination_gardes_arrears['status'] = 1;
        $where_elimination_gardes_arrears['give_status'] = 1;
        $where_elimination_gardes_arrears['arrears_status'] = 1;


        //时间段
        if (isset($param['start_date_class']) && '' !== $param['start_date_class'] && isset($param['end_date_class']) && '' !== $param['end_date_class']) {
            $where_elimination_gardes['class_date'] = array('between', array($param['start_date_class'],$param['end_date_class']));
            $where_elimination_gardes_arrears['class_date'] = array('between', array($param['start_date_class'],$param['end_date_class']));
        }

        $elimination_1v1_amount = 0;
        $elimination_1v1_amount_arrears = 0;
        $elimination_1v1_nums = 0;
        $elimination_1v1_nums_arrears = 0;
        $elimination_1v1_duration = 0;
        $elimination_1v1_duration_arrears = 0;
        $duration_1v1 = 0;


        $elimination_1vn_amount = 0;
        $elimination_1vn_amount_arrears = 0;
        $elimination_1vn_nums = 0;
        $elimination_1vn_nums_arrears = 0;
        $elimination_1vn_duration = 0;
        $elimination_1vn_duration_arrears = 0;
        $duration_1vn = 0;


        $elimination_class_amount = 0;
        $elimination_class_amount_arrears = 0;
        $elimination_class_nums = 0;
        $elimination_class_nums_arrears = 0;
        $elimination_class_duration = 0;
        $elimination_class_duration_arrears = 0;
        $duration_class = 0;

        $listData = self::where($where)->order('create_time desc')
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        if (!empty($listData)) {
            $GradesDetailsListsModel = new GradesDetailsListsModel();
            $StudentSubjectInfosClassModel = new StudentSubjectInfosClassModel();
            $CampusModel = new CampusModel();
            foreach ($listData['data'] as &$item) {
                $res_course = CourseModel::get($item['course_id']);
                $item['grades_type'] = DictionaryModel::get(['id' => $res_course['course_type']])['name'];



                $item['course_name'] = $res_course['course_name'];
                $res_list = $GradesDetailsListsModel->where(['class_manage_id' => $item['id'],'status' => 1])->select();
                $class_total_length = 0;
                foreach ($res_list as $value){
                    $start = strtotime($value['class_subject_start']);  //开始时间
                    $end = strtotime($value['class_subject_end']);  //结束时间
                    $class_total_length = $class_total_length + ceil(($end - $start)/60);
                }
                $class_total_length = $class_total_length /60; //小时
                $item['class_total_length'] = $class_total_length;

                //欠费课消
                $where_elimination_gardes_arrears['class_subject_id'] = $item['id'];
                $res_arrears_total_amount = $StudentSubjectInfosClassModel->where($where_elimination_gardes_arrears)->sum('arrears_amount');  //费用
                $res_arrears_total_amount = round(abs($res_arrears_total_amount / 100),2);

                $res_arrears_total_nums = $StudentSubjectInfosClassModel->where($where_elimination_gardes_arrears)->count();  //次数


                $res_arrears_total_duration = $StudentSubjectInfosClassModel->where($where_elimination_gardes_arrears)->sum('duration');  //时长
                $res_arrears_total_duration = $res_arrears_total_duration / 60; //小时

                $item['arrears_total_class'] = $res_arrears_total_amount.'(元)/'.$res_arrears_total_nums.'(人次)'.'/'.$res_arrears_total_duration.'(小时)';



                //总课消
                $where_elimination_gardes['class_subject_id'] = $item['id'];
                $res_total_amount = $StudentSubjectInfosClassModel->where($where_elimination_gardes)->sum('amount_deducted');  //费用
                $res_total_amount = round(abs($res_total_amount / 100),2);

                $res_total_nums = $StudentSubjectInfosClassModel->where($where_elimination_gardes)->count();  //次数


                $res_total_duration = $StudentSubjectInfosClassModel->where($where_elimination_gardes)->sum('duration');  //时长
                $res_total_duration = $res_total_duration / 60; //小时

                $item['total_class'] = $res_total_amount.'(元)/'.$res_total_nums.'(人次)'.'/'.$res_total_duration.'(小时)';


                if($res_course['course_type'] == 41){  //1v1
                    $duration_1v1 = $duration_1v1 + $class_total_length;  //时长

                    $elimination_1v1_amount_arrears = $elimination_1v1_amount_arrears + $res_arrears_total_amount;
                    $elimination_1v1_nums_arrears = $elimination_1v1_nums_arrears + $res_arrears_total_nums;
                    $elimination_1v1_duration_arrears = $elimination_1v1_duration_arrears + $res_arrears_total_duration;

                    $elimination_1v1_amount = $elimination_1v1_amount + $res_total_amount;
                    $elimination_1v1_nums = $elimination_1v1_nums + $res_total_nums;
                    $elimination_1v1_duration = $elimination_1v1_duration + $res_total_duration;


                }else if($res_course['course_type'] == 43){  //1vn

                    $duration_1vn = $duration_1vn + $class_total_length;  //时长

                    $elimination_1vn_amount_arrears = $elimination_1vn_amount_arrears + $res_arrears_total_amount;
                    $elimination_1vn_nums_arrears = $elimination_1vn_nums_arrears + $res_arrears_total_nums;
                    $elimination_1vn_duration_arrears = $elimination_1vn_duration_arrears + $res_arrears_total_duration;

                    $elimination_1vn_amount = $elimination_1vn_amount + $res_total_amount;
                    $elimination_1vn_nums = $elimination_1vn_nums + $res_total_nums;
                    $elimination_1vn_duration = $elimination_1vn_duration + $res_total_duration;


                }else if($res_course['course_type'] == 42){  //班课

                    $duration_class = $duration_class + $class_total_length;  //时长

                    $elimination_class_amount_arrears = $elimination_class_amount_arrears + $res_arrears_total_amount;
                    $elimination_class_nums_arrears = $elimination_class_nums_arrears + $res_arrears_total_nums;
                    $elimination_class_duration_arrears = $elimination_class_duration_arrears + $res_arrears_total_duration;

                    $elimination_class_amount = $elimination_class_amount + $res_total_amount;
                    $elimination_class_nums = $elimination_class_nums + $res_total_nums;
                    $elimination_class_duration = $elimination_class_duration + $res_total_duration;

                }

                $item['unit'] = $res_course['unit'];
                $item['preoccupation'] = $item['preoccupation'] ? $item['preoccupation'] : '不限';
                $item['holiday_status'] = $item['holiday_status'] ? '是' : '否';
                if($item['graduation_status'] == 0){
                    $item['status_text'] = $item['status'] ? '<span style="color:#01AAED;font-weight: bold;">已开班</span>' : ' <span  style="color:#FF5722;font-weight: bold;">未开班</span>';
                }else if($item['graduation_status'] == 1){
                    $item['status_text'] = $item['status'] ? '<span style="color:#000000;font-weight: bold;">已开班</span>' : ' <span  style="color:#000000;font-weight: bold;">未开班</span>';
                }

                if($item['repeat_status'] == 1){
                    $item['repeat_status'] = '按'.$item['interval_value'].'周重复一次';
                }

                $item['already_arranged'] = $GradesDetailsListsModel->where(['class_manage_id' => $item['id'],'status' => 1])->count();
                if($item['repeat_status'] == 2){
                    $item['repeat_status'] = '按'.$item['interval_value'].'日重复一次';
                }
                $item['graduation_status'] = $item['graduation_status'] ? '<span style="color:#FF5722;font-weight: bold;font-size:16px;">已结业</span>' : ' <span  style="color:#01AAED;font-weight: bold;font-size:16px;">未结业</span>';
                $item['campus_name'] = $CampusModel->get(['id' => $item['campus_id']])['campus_name'];

                /****首次上课日期*/
                $res = $GradesDetailsListsModel::get(['class_manage_id' => $item['id']]);
                $item['first_class_date'] = $res['weekday'].$res['class_subject_start'].'-'.$res['class_subject_end'];

            }
        }

        if($type == 1001){
            //1v1
            $item_ = [];
            $item_['grades_name'] = '1V1合计';
            $item_['headmaster'] = '-';
            $item_['grades_type'] = '-';
            $item_['class_start_date'] = '-';
            $item_['course_name'] = '-';
            $item_['unit'] = '-';
            $item_['already_arranged'] = '-';
            $item_['class_total_length'] = $duration_1v1;
            $item_['arrears_total_class'] = $elimination_1v1_amount_arrears.'(元)/'.$elimination_1v1_nums_arrears.'(人次)'.'/'.$elimination_1v1_duration_arrears.'(小时)';;
            $item_['total_class'] = $elimination_1v1_amount.'(元)/'.$elimination_1v1_nums.'(人次)'.'/'.$elimination_1v1_duration.'(小时)';;
            $listData['data'][] =   $item_;

            //1vn
            $item_ = [];
            $item_['grades_name'] = '班课合计';
            $item_['headmaster'] = '-';
            $item_['grades_type'] = '-';
            $item_['class_start_date'] = '-';
            $item_['course_name'] = '-';
            $item_['unit'] = '-';
            $item_['already_arranged'] = '-';
            $item_['class_total_length'] = $duration_1vn;
            $item_['arrears_total_class'] = $elimination_1vn_amount_arrears.'(元)/'.$elimination_1vn_nums_arrears.'(人次)'.'/'.$elimination_1vn_duration_arrears.'(小时)';;
            $item_['total_class'] = $elimination_1vn_amount.'(元)/'.$elimination_1vn_nums.'(人次)'.'/'.$elimination_1vn_duration.'(小时)';;
            $listData['data'][] =   $item_;


            //班课
            $item_ = [];
            $item_['grades_name'] = '1VN合计';
            $item_['headmaster'] = '-';
            $item_['grades_type'] = '-';
            $item_['class_start_date'] = '-';
            $item_['course_name'] = '-';
            $item_['unit'] = '-';
            $item_['already_arranged'] = '-';
            $item_['class_total_length'] = $duration_class;
            $item_['arrears_total_class'] = $elimination_class_amount_arrears.'(元)/'.$elimination_class_nums_arrears.'(人次)'.'/'.$elimination_class_duration_arrears.'(小时)';;
            $item_['total_class'] = $elimination_class_amount.'(元)/'.$elimination_class_nums.'(人次)'.'/'.$elimination_class_duration.'(小时)';;
            $listData['data'][] =   $item_;


        }

        return $listData;
    }
}